package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.lifecycle.Transformations
import com.cheyun.netsalev3.bean.AllocatedListInfo
import com.cheyun.netsalev3.repository.AllocatedListPageRepository
import com.cheyun.netsalev3.utils.ToastUtil
import com.cheyun.netsalev3.view.defeatdetails.DefeatDetailsActivity

class AllocatedListViewModel(var repository: AllocatedListPageRepository) : BaseViewModel() {
     var items= repository.loadAndroidData()

    val networkState = Transformations.switchMap(items) { repository.networkState }!!
    val total = Transformations.switchMap(items) { repository.total }!!

    fun onclickItem(view: View, item: AllocatedListInfo) {
        if(item.tid == 0){
            ToastUtil.toastShortMessage("该线索正在建档中，请稍候再操作")
        }else{
            var intent = Intent(view.context, DefeatDetailsActivity::class.java)
            intent.putExtra("tid", item.tid)
            intent.putExtra("nkey", "archive.index")
            view.context.startActivity(intent)
        }
    }

    fun refresh(){
        repository.dataSourceFactory.sourceLiveData.value?.invalidate()
    }

//    fun refresh(today:String) {
//        var hashMap = HashMap<String, String>()
//        repository?.map!!.forEach {
//            hashMap[it.key] = it.value
//        }
//        if(today.isNotEmpty()) {
//            hashMap["etime"] = today
//            repository.changeMap(hashMap)
//            hashMap["stime"] = today
//        }else{
//            if(hashMap.containsKey("stime")){
//                hashMap.remove("stime")
//                hashMap.remove("etime")
//                repository.changeMap(hashMap)
//            }
//        }
//        repository.dataSourceFactory?.sourceLiveData?.value?.invalidate()
//    }

    fun refresh(hashMap:HashMap<String, String>) {
//        var hashMap = HashMap<String, String>()
//        repository?.map!!.forEach {
//            hashMap[it.key] = it.value
//        }
//        if(today.isNotEmpty()) {
//            hashMap["stime"] = today
//            hashMap["etime"] = today
//            repository.changeMap(hashMap)
//        }else{
//            if(hashMap.containsKey("stime")){
//                hashMap.remove("stime")
//                hashMap.remove("etime")
//                repository.changeMap(hashMap)
//            }
//        }
        repository.changeMap(hashMap)
        repository.dataSourceFactory?.sourceLiveData?.value?.invalidate()
    }



}
